import { _decorator, Component, Node } from 'cc';
import { DataManager } from '../managers/DataManager';
import { GlobalManager } from './GlobalManager';
import { CommonUtils } from '../utils/CommonUtils';
import NetworkManager, { SeverUrl } from '../managers/NetworkManager';
const { ccclass, property } = _decorator;
class UserData {
    public userName: string = null;
    public openId: string = "";
    public loadtime: number = 0;
    public SoundVolume: number = 1;
    public MusicVolume: number = 1;


}

@ccclass('UserDate')
export class UserDate {
    public key: string = null;
    public needServerSave: boolean = false;

    public static instance: UserDate = null;
    public userData: UserData = null;

    public static getInstance(): UserDate {
        if (this.instance == null) {
            this.instance = new UserDate();
        }
        return this.instance;
    }

    constructor() {
        this.initData();
        this.setNewDayData();
    }

    /**初始化数据 */
    initData() {
        this.key = GlobalManager.gameName;
        let value: UserData = DataManager.instance.loadLocal(this.key);
        let srcValue = new UserData();
        value != null ? this.userData = value : this.userData = new UserData();
        for (let k in srcValue) {
            if (this.userData[k] == null) {
                this.userData[k] = srcValue[k];
            }
        }

        if (this.needServerSave && this.userData.openId) {

            //获取服务器存档数据
            NetworkManager.getServerInfo({
                game: GlobalManager.gameName,
                openId: this.userData.openId,

            }, (a, b) => {
                if (a && b.code == 200) {
                    this.userData = JSON.parse(b.data);
                    DataManager.instance.saveLocal(this.key, this.userData);
                }
            }, SeverUrl.getServerInfo, true);
        } else {
            console.error("this.userData.openId===", this.userData.openId)

        }



    }

    /**新的一天刷新某些数据 */
    private setNewDayData() {
        if (this.userData.loadtime && CommonUtils.isNewDay(this.userData.loadtime)) {

            this.save();
        } else {
            this.userData.loadtime = Date.now();
            this.save();
        }
    }



    /**获取用户名 */
    set userName(value: string) {
        this.userData.userName = value;
        this.save()
    }
    /**获取用户名 */

    get userName(): string {
        return this.userData.userName;
    }

    /**openId */
    set openId(value: string) {
        this.userData.openId = value;
        this.save()
    }
    /**openId */
    get openId(): string {
        return this.userData.openId;
    }

    /*音效大小 */
    set SoundVolume(value: number) {
        this.userData.SoundVolume = value;
        this.save()

    }

     /*音效大小 */
    get SoundVolume() {
        return this.userData.SoundVolume
    }

    /*音乐大小 */
    set MusicVolume(value: number) {
        this.userData.MusicVolume = value;
        this.save()

    }

     /*音乐大小 */
    get MusicVolume() {
        return this.userData.MusicVolume
    }


    

    saveTime = 0
    /**保存 */
    save(qianzhi: boolean = false) {
        DataManager.instance.saveLocal(this.key, this.userData);
        if (this.needServerSave) {
            if (this.saveTime == 0) {
                this.saveTime = Date.now();
            }
            if (Date.now() - this.saveTime > 30000 || qianzhi) {
                this.saveTime = Date.now();
                //保存服务器数据
                NetworkManager.getServerInfo({
                    game: GlobalManager.gameName,
                    openId: this.userData.openId,
                    data: JSON.stringify(this.userData)
                }, (a, b) => {
                    if (a && b.code == 200) {
                        console.log("保存服务器数据成功", b.data);
                    }
                }, SeverUrl.saveServerInfo, true);
            }

        }
    }
}


